<?php
namespace app\api\controller;

use app\api\common\controller\Api;
use hg\apidoc\annotation as Apidoc;
use app\common\library\EasyWechat;
use think\cache\driver\Redis;
use think\facade\Config;
use think\facade\Cache;
use app\api\common\controller\LuckDraw;
use think\facade\Db;
use think\facade\Request;

/**
 * @Apidoc\Title("Demo.php")
 * @Apidoc\Group("api")
 */
class Store extends Api
{

    /**
     * 无需登录的方法,同时也就不需要鉴权了
     * @var array
     */
    protected $noNeedLogin = ['*'];
    /**
     * 无需鉴权的方法,但需要登录
     * @var array
     */
    protected $noNeedRight = [];
    //基础信息
    public function getLists(){

        $data=Db::name('store')->where('id',1)->find();
        $data['image']=$data['image']?explode('|',$data['image']):"";
        $data['payment_method']=$data['payment_method']?explode(',',$data['payment_method']):"";
        $data['characteristic']=$data['characteristic']?explode(',',$data['characteristic']):"";
         $this->result('success', $data,200,'json');
    }
    //门票列表
     public function getTticketList(){
        $ticke_type=Db::name('ticket_type')->where('status',1)->select()->toArray();
        foreach ($ticke_type as $k => $v) {
            $ticke_type[$k]['total_number']=$this->getOrderNumber($v['id'],1);
            $ticke_type[$k]['number']=$this->getOrderNumber($v['id'],2);
            $childern=Db::name('ticket')->where('type',$v['id'])->where('status',1)->where('store_id',1)->select()->toArray();
              $d=date('Y-m-d',time());
              $times=strtotime($d);
            foreach ($childern as $kk => $vv) {
                $price=Db::name('ticket_price')->where('tree_ticket_id',$vv['id'])->where('create_time',$times)->find();
                if($price){
                        $childern[$kk]['price']=$price['price'];
                        $childern[$kk]['activity_price']=$price['activity_price'];
                }
                
            }
            $ticke_type[$k]['childern']=$childern;
        }

        $data['list']=$ticke_type;
        $data['total']=array_sum(array_column($ticke_type,'number'));
       $this->result('success',$data,200,'json');
     }
     //已售数量
     public function getOrderNumber($type,$day=1){
            $ticket_id=Db::name('ticket')->where('type',$type)->column('id');
            $where=[ 
                ['orders_status','in',[1,2]],   
            ];
            if($day==2){
                $start = mktime(0,0,0,date('m'),date('d'),date('Y'));
                $end_time =mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;
                array_push($where,['create_time','between',[$start,$end_time]]);
            }
            $order_id=Db::name('order')->where($where)->column('id');
            $whereserver=[
                ['server_id','in',$ticket_id],
                ['order_id','in',$order_id]
            ];
            if($day==2){
                array_push($whereserver, ['type','=',1]);
            }
            $count=Db::name('order_server')->where($whereserver)->sum('number');
            return $count;
     }
     //游客信息列表
     public function getUserLists(){
           $member_id=input('member_id');
           if (!$member_id || $member_id == 0) {
              $this->result('请先登录', [],400,'json');
           }
           $data=Db::name('user')->where('member_id',$member_id)->select()->toArray();
           foreach ($data as $k => $v) {
               $data[$k]['checked']=false;
           }
       
        $this->result('success', $data,200,'json');
     }
     //新增编辑游客
     public function addUser(){
        $post = request()->param();
            $rule = [
                'name|姓名'               => 'require',
                'phone|联系电话'          => 'require|number|mobile',
                'member_id|member_id参数' =>'require',
            ];
            $msg=$this->validate($post, $rule);
            if($msg!==true) $this->result($msg, [],400,'json');
            $member_id=$post['member_id'];
            $name=$post['name'];
            $phone=$post['phone'];
            $is_use=$post['is_use'];

        $where=[
            ['phone','=',$phone]
        ];
        if(isset($post['id'])) array_push($where, ['id','not in',[$post['id']]]);
        $count=Db::name('user')->where($where)->fetchsql(false)->count();
        if($count) $this->result('该手机号已存在',[],400,'json');
        $data=[
            'name'=>$name,
            'phone'=>$phone,
            'member_id'=>$member_id,
            'is_use'=>$is_use?$is_use:0,
        ];
        if(isset($post['id'])){
            // if($is_use==1){
            //     Db::name('user')->where('member_id',$member_id)->update(['is_use'=>0]);
            // }
            $res=Db::name('user')->where('id',$post['id'])->update($data);
           
            if($res) $this->result('编辑成功',[],200,'json');
            $this->error('编辑失败', '',400,'json');
        }else{
            $res=Db::name('user')->insert($data);
            if($res) $this->result('新增成功',[],200,'json');
            $this->result('新增失败', [],400,'json');
        }
     }
     //删除游客信息
     public function delUser(){
        $member_id=input('member_id');
        $id=input('id');
        $where=[
            ['member_id','=',$member_id],
            ['id','=',$id]
        ];
        if(!$member_id || $member_id==0){
            $this->result('参数错误',[],400,'json');
        }
        if(!$id || $id==0){
            $this->result('参数错误',[],400,'json');
        }
        $res=Db::name('user')->where($where)->delete();
        if($res) $this->result('删除成功', '',200,'json');
         $this->result('删除失败', '',400,'json');
     }
     //票价格信息
     public function getTicketPrice(){
        $ticket_id=input('ticket_id');
        $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
        $mint=date("Y-m-d", $tomorrow);//明天
        $after=mktime(0,0,0,date("m"),date("d")+2,date("Y"));
        $after_day=date("Y-m-d", $after);//后天
        $array = array("日","一","二","三","四","五","六");//$array[date('w')]
        if(!$ticket_id || $ticket_id==0){
            $this->result('参数错误', [],400,'json');
        }
        $where=[
            ['tree_ticket_id','=',$ticket_id],
            ['create_time','between time',[strtotime(date('Y-m-d')),$after]]
        ];
        $data=Db::name('ticket_price')->where($where)->order('create_time asc')->select()->toArray();
        foreach ($data as $k => $v) {
            $data[$k]['server_type']=1;
           $data[$k]['day']=date('m-d',$v['create_time']);
           if(date('d',$v['create_time'])==date('d')){
              $data[$k]['week']="今天";
           }elseif(date('d',$v['create_time'])==date('d')+1){
               $data[$k]['week']="明天";
           }elseif(date('d',$v['create_time'])==date('d')+2){
                $data[$k]['week']="周".$array[date('w',$v['create_time'])];
           }
        }
        $this->result('success', $data,200,'json');
     }
     /**
      * 日历价格
      * */
     public function getPriceLists(){
        $data=input('get.');
          $ticket_id=$data['ticket_id'];
          $activity=isset($data['type'])?$data['type']:0;
          if(!$ticket_id || $ticket_id==0){
            $this->result('参数错误',[],400,'json');
           }
           $day7=mktime(0,0,0,date("m"),date("d")+7,date("Y"));
           $where=[
            ['tree_ticket_id','=',$ticket_id],
            ['create_time','>=',strtotime(date('Y-m-d'))],
            ['create_time','<',$day7]
         ];
          $array = array("日","一","二","三","四","五","六");
          $data=Db::name('ticket_price')->where($where)->order('create_time asc')->select()->toArray();
          foreach ($data as $k => $v) {
             $data[$k]['server_type']=1;
             $data[$k]['checked']=false;
              $data[$k]['day']=date('m-d',$v['create_time']);
               $data[$k]['day']=date('m-d',$v['create_time']);
               if(date('d',$v['create_time'])==date('d')){
                  $data[$k]['week']="今天";
                  $data[$k]['checked']=true;
               }elseif(date('d',$v['create_time'])==date('d')+1){
                   $data[$k]['week']="明天";
               }else{
                    $data[$k]['week']="周".$array[date('w',$v['create_time'])];
               }
          }
          //  if($activity==1){
          //    // 1661529600
          //    foreach ($data as $k => $v) {
          //       $d=date('Y-m-d',time());
          //        $times=strtotime($d);
          //       if($times<=1661529600 && $v['create_time']==1661529600){
          //               $data[$k]['checked']=true;
          //       }elseif(1661529600<$times && $times<=1661616000 && $v['create_time']==1661616000){
          //               $data[$k]['checked']=true;
          //       }elseif(1661616000<$times && $times<=1661702400 && $v['create_time']==1661702400){
          //               $data[$k]['checked']=true;
          //       }else{
          //           $data[$k]['checked']=false;
          //       }
          //    }
          // }
          if(in_array($ticket_id,[4,5,6,7])){
                foreach ($data as $k => $v) {
                        if(in_array($v['create_time'],[1661529600,1661616000,1661702400])){
                                unset($data[$k]);
                        }
                }
          }
          
          $this->result('success', $data,200,'json');
     } 
     // 近7天时间
     public function getDateDay(){
         $day7=mktime(0,0,0,date("m"),date("d")+7,date("Y"));
         $array = array("日","一","二","三","四","五","六");
         $time=time();
          $date = [];
         for ($i=0; $i<=6; $i++) {
            $date[$i]['time'] =strtotime('+' . $i .' days', $time);
        }
        
         foreach ($date as $k => $v) {
            if(date('d',$v['time'])==date('d')){
                  $date[$k]['week']="今天";
               }elseif(date('d',$v['time'])==date('d')+1){
                   $date[$k]['week']="明天";
               }else{
                    $date[$k]['week']="周".$array[date('w',$v['time'])];
               }
               $date[$k]['checked']=false;
               $date[$k]['time']=date('Y-m-d',$v['time']);
         }
         $this->result('success', $date,200,'json');
     }
     //客户列表
     public function getCustomerList(){
        $post=Request()->param();
        $page=$post['page']?$post['page']:1;
        $size=$post['size']?$post['size']:20;
        $where=[];
        if($post['keywords']){
            array_push($where,['name|phone','like','%'.$post['keywords'].'%']);
        }
        $data=Db::name('user')->where($where)->page($page,$size)->select()->toArray();
        $this->result('success', $data,200,'json');
     }
}